<?php

/**
 * ringtone_xml actions.
 *
 * @package    sf_sandbox
 * @subpackage ringtone_xml
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class ringtone_xmlActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    #$this->forward('default', 'module');
    return $this->forward('ringtone_xml', 'list');
    #return $this->redirect('ringtone_xml/list/?clear=yes');
  }
  public function executeList()
  {
  	
  	$lastweek = date('Y:m:d H:i:s', ( time() - (10 * 24 * 60 * 60)) );
  	$tr = new Criteria();
  	$tr->add(RingtonePeer::RBT_STATUS,1);
  	#$tr->add(RingtonePeer::CREATED_DATETIME,$lastweek,Criteria::GREATER_EQUAL);
  	
  	$tr->addDescendingOrderByColumn(RingtonePeer::TOTAL_SET);
  	$tr->setLimit(20);
  	$list_topring =  RingtonePeer::doSelect($tr);
  	
  	
  	// Neu la lan dau load page - Xoa cac DL cu~ va chen 20 top ringtone
  	$status_del = $this->getRequest()->getParameter('clear');
  	if (!$status_del || $status_del == 'yes'){
  		RingtoneXmlPeer::doDeleteAll();
  		
  		$i = 1;
  		foreach($list_topring as $item){
  			if($item->getAvatarPath()){
  				$image_thumb = MEDIA_DOMAIN.'/ringtone/thumbnail/large/'.$item->getAvatarPath();
  			}else{
  				$image_thumb = HOST_DOMAIN.'/images/default_ringtone_75.png';
  			}
  			
  			$ringtonexml = new RingtoneXml();
  			$ringtonexml->setRingtoneid($item->getId());
  			$ringtonexml->setName($item->getName());
  			$ringtonexml->setSinger($item->getCreatorName());
  			#$ringtonexml->setSinger($item->getCreatedDatetime());
  			$ringtonexml->setImage($image_thumb);
  			$ringtonexml->setUrl(HOST_DOMAIN.'/nhac-chuong/'. iring::str_normalizer_c($item->getName()).'/'. $item->getId());
  			$ringtonexml->setRbtcode($this->_getRbtcode($item->getId()));
  			$ringtonexml->setRtid($item->getId());
  			$ringtonexml->setDuration($item->getDuration());
  			$ringtonexml->setOrdering($i);
  			#echo "<pre>";print_r($ringtonexml);exit();
  			$ringtonexml->save();
  			$i++;
  		}
  	}
  	
  	$xml_rt = new Criteria();
  	$xml_rt->addAscendingOrderByColumn(RingtoneXmlPeer::ORDERING);
  	$this->listxml =  RingtoneXmlPeer::doSelect($xml_rt);
  	
  }
  public function executeSearch()
  {
  	#$this->setLayout('none');
  	
  	/*$name =  $this->getRequest()->getParameter('name');
  	$tr = new Criteria();
  	$tr->add(RingtonePeer::NAME, strtr($name, '*', '%') . '%', Criteria::LIKE );
  	$tr->addOr(RingtonePeer::RBT_STATUS, 1);
  	$this->list_res =  RingtonePeer::doSelect($tr);*/
  	
  	
  }
  
  public function executeAdd2list()
  {
  	$id =  $this->getRequest()->getParameter('id');
  	$tr = new Criteria();
  	$tr->add(RingtonePeer::ID, $id);
  	$aring =  RingtonePeer::doSelectOne($tr);
  	
    if($aring->getAvatarPath()){
  			$image_thumb = MEDIA_DOMAIN.'/ringtone/thumbnail/large/'.$aring->getAvatarPath();
  		}else{
  			$image_thumb = HOST_DOMAIN.'/images/default_ringtone_75.png';
  		}
  	
  	// Get max order
  	$con = new MysqlDAL('propel');
    $res = $con->doSelect("SELECT MAX(ordering) AS max FROM ringtone_xml");
	$maxorder = $res[0]['max'];
    
  	$ringtonexml = new RingtoneXml();
  	$ringtonexml->setRingtoneid($aring->getId());
  	$ringtonexml->setName($aring->getName());
  	$ringtonexml->setSinger($aring->getCreatorName());
  	$ringtonexml->setImage($image_thumb);
  	$ringtonexml->setUrl(HOST_DOMAIN.'/nhac-chuong/'. iring::str_normalizer_c($aring->getName()).'/'. $aring->getId());
  	$ringtonexml->setRbtcode($this->_getRbtcode($aring->getId()));
  	$ringtonexml->setRtid($aring->getId());
  	$ringtonexml->setDuration($aring->getDuration());
  	#$ringtonexml->setOrdering( RingtoneXmlPeer::doCount(new Criteria()) + 1);
  	$ringtonexml->setOrdering($maxorder + 1);
  	$ringtonexml->save();
  	
  	$xml_rt = new Criteria();
  	$xml_rt->addAscendingOrderByColumn(RingtoneXmlPeer::ORDERING);
  	$this->listxml =  RingtoneXmlPeer::doSelect($xml_rt);
  }
  
  public function executeDelrecord()
  {
  	$id = $this->getRequest()->getParameter('id');
  	$ringtone_xml = RingtoneXmlPeer::retrieveByPK($id);
  	$ringtone_xml->delete();
  	
  	$xml_rt = new Criteria();
  	$xml_rt->addAscendingOrderByColumn(RingtoneXmlPeer::ORDERING);
  	$this->listxml =  RingtoneXmlPeer::doSelect($xml_rt);
  }
  
  public function executeReorder()
  {
  	$order = $this->getRequest()->getParameter('order');
  	$listxml =  RingtoneXmlPeer::doSelect(new Criteria());
  	foreach ($listxml as $item){
  		#echo $order[$item->getRingtoneid()].'<hr>';
  		if($item->getordering() != $order[$item->getRingtoneid()]){
  			$ringtone_xml = RingtoneXmlPeer::retrieveByPK($item->getRingtoneid());
  			$ringtone_xml->setOrdering($order[$item->getRingtoneid()]);
  			$ringtone_xml->save();
  		}
  	}
  	
  	return $this->redirect('ringtone_xml/list/?clear=no');
  }
  
  public function executeSavefile()
  {
  	$rtxml = new Criteria();
  	$rtxml->addAscendingOrderByColumn(RingtoneXmlPeer::ORDERING);
  	$listxml =  RingtoneXmlPeer::doSelect($rtxml);
  	$numRt = $this->getRequest()->getParameter('numRt',4);        
  	$doc = new DOMDocument('1.0', 'UTF-8');
  	$doc->formatOutput = true;
  	$Items = $doc->createElement( "data" );
  	$doc->appendChild( $Items );
  	$index = 0;
  	foreach( $listxml as $row ) {
		  $ItemInfo = $doc->createElement( "item" );

		  $image = $doc->createElement("src");
		  $image->appendChild($doc->createTextNode('<![CDATA['.HOST_DOMAIN.'/flash/images/'.$row->getBackground().']]>'));
		  $ItemInfo->appendChild($image);
		  
		  $name = $doc->createElement("bigTitle");
		  $name->appendChild($doc->createTextNode('<![CDATA['.$row->getName().']]>'));
		  $ItemInfo->appendChild($name);
		  
		  $detailTitle = $doc->createElement("detailTitle");
		  $detailTitle->appendChild($doc->createTextNode('<![CDATA['.$row->getSinger().']]>'));
		  $ItemInfo->appendChild($detailTitle);
		  
		  $url = $doc->createElement("URL");
		  $url->appendChild($doc->createTextNode('<![CDATA['.$row->getUrl().']]>'));
		  $ItemInfo->appendChild($url);
		  
		  $rtId = $doc->createElement("rtId");
		  $rtId->appendChild($doc->createTextNode('<![CDATA['.$row->getRtId().']]>'));
		  $ItemInfo->appendChild($rtId);
		  
		  $ringtone = RingtonePeer::retrieveByPK($row->getRtId());
		  $file = $doc->createElement("file");
		  $file->appendChild($doc->createTextNode('<![CDATA['.MEDIA_DOMAIN.'/ringtone/realtone/'.$ringtone->getFilePathMp3().']]>'));
		  $ItemInfo->appendChild($file);
		  
		  $Items->appendChild( $ItemInfo );
		  
		  $index++;
		  if ($index >= $numRt) break;
	 }
     #$doc->save("xmlfile/imuzik_sangtao.xml");
     $content = $doc->saveXML();
	 $content = str_replace("&lt;", "<", $content);
	 $content = str_replace("&gt;", ">", $content);
	 
	 // Sua lai path khi upload
	 $path = sfConfig::get('banner_xml_path');
	 $file = fopen($path,"w");
	 fwrite($file,$content);        
	 fclose($file);
	 
	 
	// fetured
	
    $rtxml = new Criteria();
    $rtxml->add(RingtoneXmlPeer::BACKGROUND, "", Criteria::NOT_EQUAL);
  	$rtxml->addAscendingOrderByColumn(RingtoneXmlPeer::ORDERING);
  	$listxml =  RingtoneXmlPeer::doSelect($rtxml);
  	
	$doc2 = new DOMDocument('1.0', 'UTF-8');
  	$doc2->formatOutput = true;
  	$sangtao = $doc2->createElement( "Featured" );
  	$doc2->appendChild( $sangtao );
  	$Items = $doc2->createElement( "Items" );
  	$sangtao->appendChild( $Items );
  	foreach( $listxml as $row ) {
		  $ItemInfo = $doc2->createElement( "ItemInfo" );
		  
		  $type = $doc2->createElement( "type" );
		  $type->appendChild($doc2->createTextNode( '<![CDATA[ringtone]]>' ));
		  $ItemInfo->appendChild($type);
		  
		  $category = $doc2->createElement( "category" );
		  $category->appendChild($doc2->createTextNode('<![CDATA[Music Ringtone]]>'));
		  $ItemInfo->appendChild( $category );
		  
		  $name = $doc2->createElement("name");
		  $name->appendChild($doc2->createTextNode('<![CDATA['.$row->getName().']]>'));
		  $ItemInfo->appendChild($name);
		  
		  $image = $doc2->createElement("image");
		  $image->appendChild($doc2->createTextNode('<![CDATA[http://sangtao.imuzik.com.vn/featured/'.$row->getBackground().']]>'));
		  $ItemInfo->appendChild($image);
		  
		  $url = $doc2->createElement("URL");
		  $url->appendChild($doc2->createTextNode('<![CDATA['.$row->getUrl().']]>'));
		  $ItemInfo->appendChild($url);
		  
		  $duration = $doc2->createElement("duration");
		  $duration->appendChild($doc2->createTextNode('<![CDATA['.$row->getDuration().']]>'));
		  $ItemInfo->appendChild($duration);
		  
		  $Items->appendChild( $ItemInfo );
	 }
	 #$doc2->save("xmlfile/featured.xml");
	 
	 $content = $doc2->saveXML();
	 $content = str_replace("&lt;", "<", $content);
	 $content = str_replace("&gt;", ">", $content);
	 // Sua lai path khi upload
	 $path = sfConfig::get('portable_banner_xml_path');
	 $file = fopen($path,"w");
	 fwrite($file,$content);
	 fclose($file);
  }
  
  
  public function executeUpload()
  {
  	$this->setlayout('popup');
  	$this->ringtoneid =  $this->getRequest()->getParameter('id');
  	$this->msg = $this->getRequest()->getParameter('msg');        
  	
  }
  
 public function executeUploadsave()
  {
  	$this->setlayout('popup');
  	$id =  $this->getRequest()->getParameter('id');
  	// Sua lai path khi upload
  	$uploaddir = sfConfig::get('images_banner_xml_path');
	$uploadfile = $uploaddir.'/'. basename($_FILES['image']['name']);
	#die($_FILES['image']['type']);
	$filetype = $_FILES['image']['type'];
	$typeAccepted = array("image/jpeg", "image/gif", "image/png");
	if(in_array($filetype,$typeAccepted)) {    
		if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
			  $ringtone_xml = RingtoneXmlPeer::retrieveByPK($id);
		  	  $ringtone_xml->setBackground( (string) basename($_FILES['image']['name']));
		  	  $ringtone_xml->save();
		} else {
			  $err =  'Xẩy ra lỗi khi upload';
			  return $this->redirect('ringtone_xml/upload?id='.$id.'&msg='.$err);
		}
	}else{
		 $err =  'Chỉ upload file ảnh';
	     return $this->redirect('ringtone_xml/upload?id='.$id.'&msg='.$err);
	}
  }
  
  private function _getRbtcode($ringtoneId)
  {
  	$rt = new Criteria();
  	$rt->add(RingtoneExtraPeer::RINGTONE_ID,$ringtoneId);
  	$row = RingtoneExtraPeer::doSelectOne($rt);
  	return $row->getRbtCode();
  	
  }
}
